/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.repository;

import dao.ConnectionManager;
import domain.EccezioneUpdater;
import domain.File;
import domain.Root;
import domain.TipoLinkIn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alessio
 */
public class FilesRepository {
    private static final String FIND_BY_ROOT_SQL = "SELECT * FROM file,contained_in WHERE file = id_file AND root = ?";

    public static Collection<File> findFilesByRoot(Root root) throws EccezioneUpdater {
        Collection<File> res = new ArrayList<File>();
        Connection con = null;
        PreparedStatement st = null;
        try {
            con = ConnectionManager.getConnection();
            st = con.prepareStatement(FIND_BY_ROOT_SQL);
            st.setInt(1, root.getId_root());
            ResultSet rs = st.executeQuery();
            while(rs.next()){
                File f = new File();
                f.setId_file(rs.getInt("id_file"));
                f.setPath_file(rs.getString("path_file"));
                f.setIncluso(rs.getBoolean("incluso"));
                f.setUltima_modifica(rs.getTimestamp("ultima_modifica").getTime());
                f.inserisciLinkIn(new TipoLinkIn(root, f));
                res.add(f);
            }
        } catch (ClassNotFoundException ex) {
            throw new EccezioneUpdater(ex.getMessage());
        }catch(SQLException ex){
            throw new EccezioneUpdater(ex.getMessage());
        }
        return res;
    }
}
